import { defineStore } from "pinia";
import { userKey } from "./key";

interface IUserStore {
  menus: IMenuModel[];
  username: string;
}
type menu = IMenuModel & { children: IMenuModel[] };

export const useUserStore = defineStore(userKey, {
  state(): IUserStore {
    return {
      menus: [],
      username: "",
    };
  },
  getters: {
    routes() {
      let menus: menu[] = [];
      //获取一级标题
      menus = this.menus
        .filter((item) => item.parentId === 0)
        .map((item) => ({ ...item, children: [] }));
      //获取二级标题
      this.menus.forEach((element) => {
        const list = menus.find((item) => item.id === element.parentId);
        if (list) {
          list.children.push(element);
        }
      });
      return menus;
    },
    permissions(): string[] {
      return this.menus.map((item) => item.name);
    },
  },
  actions: {},
});
